//
//  DCSendingController.m
//  JiuyaoMotorcade
//
//  Created by 大橙子 on 2018/11/28.
//  Copyright © 2018 Tomous. All rights reserved.
//

#import "DCSendingController.h"
#import "DCSendingHeaderView.h"
#import "DCSendingTableViewCell.h"
#import "DCSendingFooterView.h"
#import "DCTaskDetailViewController.h"
#import "DCMapViewController.h"
@interface DCSendingController ()<UITableViewDelegate,UITableViewDataSource,sendingFooterViewDelegate>

@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) NSMutableArray *dataSource;
@property (nonatomic,assign) NSInteger page;

@property (nonatomic,strong) UIView *bgView;
@property (nonatomic,strong) UITextField *plateField;
@property (nonatomic,strong) UITextField *nameField;
@end

@implementation DCSendingController

-(UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height) style:UITableViewStyleGrouped];
        _tableView.separatorStyle = UITableViewCellStyleDefault;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = BackgroundColor;
        _tableView.mj_header = [MJRefreshStateHeader headerWithRefreshingTarget:self refreshingAction:@selector(refreshTableView)];
        _tableView.mj_footer = [MJRefreshBackStateFooter footerWithRefreshingTarget:self refreshingAction:@selector(reloadMoeData)];
    }
    return _tableView;
}
-(NSMutableArray *)dataSource
{
    if (!_dataSource) {
        _dataSource = [NSMutableArray new];
    }
    return _dataSource;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self getSendingData];
}
-(void)getSendingData {
    NSMutableDictionary *info = [NSMutableDictionary dictionary];
    info[@"taskStatus"] = @"03";
    info[@"page"] = @"1";
    info[@"limit"] = @"10";
    __weak typeof(self) weakself = self;
    [DCServiceTool postWithUrl:getPagerTasksByCarrier_URL params:info success:^(id responseObject) {
        if ([responseObject[@"code"] integerValue] == 0) {
            
            if ([responseObject[@"count"] integerValue] != 0) {
                weakself.dataSource = [DCOrderReceiveModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"]];
                [weakself.view addSubview:weakself.tableView];
                weakself.page = [responseObject[@"page"] integerValue] +1;
                [weakself setUpBgViewIsShow:NO block:nil];
            }else{
                [weakself setUpBgViewIsShow:YES block:^{
                    [weakself getSendingData];
                }];
            }
            
        }else {
            [weakself showMessage:responseObject[@"msg"] afterDelay:1.0];
        }
    } failure:^(NSError *error) {
        
    }];
}
-(void)refreshTableView {
    [self.tableView.mj_header endRefreshing];
    [self getSendingData];
}
-(void)reloadMoeData {
    [self.tableView.mj_footer endRefreshing];
    NSMutableDictionary *info = [NSMutableDictionary dictionary];
    info[@"taskStatus"] = @"03";//03 04 05 06
    info[@"page"] = @(self.page);
    info[@"limit"] = @"5";
    __weak typeof(self) weakself = self;
    [DCServiceTool postWithUrl:getPagerTasksByCarrier_URL params:info success:^(id responseObject) {
        DCLog(@"responseObject--%@",responseObject);
        if ([responseObject[@"code"] integerValue] == 0) {
            NSArray *array = [DCOrderReceiveModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"]];
            if (array.count != 0) {
                [weakself.dataSource addObjectsFromArray:array];
                weakself.page = [responseObject[@"page"] integerValue] +1;
                [weakself.tableView reloadData];
            }else{
                [weakself showMessage:@"已加载全部数据" afterDelay:1.0];
            }
            
        }else {
            [weakself showMessage:responseObject[@"msg"] afterDelay:1.0];
        }
    } failure:^(NSError *error) {
        
    }];
}
#pragma tableViewDelegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataSource.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 50;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    DCSendingHeaderView *headerView = [[DCSendingHeaderView alloc]init];
    headerView.backgroundColor = BackgroundColor;
    DCOrderReceiveModel *model = self.dataSource[section];
    headerView.model = model;
    return headerView;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 70;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    DCSendingFooterView *footerView = [[DCSendingFooterView alloc]init];
    footerView.backgroundColor = BackgroundColor;
    DCOrderReceiveModel *model = self.dataSource[section];
    footerView.model = model;
    footerView.delegate = self;
    return footerView;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    DCOrderReceiveModel *model = self.dataSource[section];
    return model.oddArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    DCSendingTableViewCell *cell = [DCSendingTableViewCell cellWithTableView:tableView];
    DCOrderReceiveModel *orderModel = self.dataSource[indexPath.section];
    cell.model = orderModel.oddArray[indexPath.row];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return (kBaseHeight-10)*3;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    DCOrderReceiveModel *model = self.dataSource[indexPath.section];
    DCTaskDetailViewController *detailVC = [[DCTaskDetailViewController alloc]initWithOrderID:model.taskId state:@"运输中"];
    [[UIViewController currentViewController].navigationController pushViewController:detailVC animated:YES];
}

#pragma sendingFooterViewDelegate
-(void)selectedWithMapBtn:(NSString *)content {
    DCLog(@"%@",content);
    DCMapViewController *mapVC = [[DCMapViewController alloc]init];
    [[UIViewController currentViewController].navigationController pushViewController:mapVC animated:YES];
}
-(void)selectedWithChangeCarBtn:(NSString *)content {
    DCLog(@"%@",content);
    [[UIApplication sharedApplication].keyWindow addSubview:self.bgView];
    //动画
    self.bgView.transform = CGAffineTransformMakeScale(0, 0);
    [UIView animateWithDuration:.5 animations:^{
        self.bgView.transform = CGAffineTransformIdentity;
    }];
}

-(UIView *)bgView {
    if (!_bgView) {
        _bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
        _bgView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:.5];;
        _bgView.userInteractionEnabled = YES;
        
//        UITapGestureRecognizer *tapBgView = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(bgViewDidClick)];
//        [_bgView addGestureRecognizer:tapBgView];
        UIView *contentView = [[UIView alloc]init];
        contentView.size = CGSizeMake(ScreenWidth*0.8, 40*5+30);
        contentView.center = _bgView.center;
        contentView.backgroundColor = [UIColor whiteColor];
        contentView.layer.cornerRadius = 10;
        [_bgView addSubview:contentView];
        
        UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, contentView.width, 40)];
        titleLabel.textAlignment = NSTextAlignmentCenter;
        titleLabel.text = @"强制换车";
        [contentView addSubview:titleLabel];
        
        UIView *line = [[UIView alloc]initWithFrame:CGRectMake(0, titleLabel.bottom, contentView.width, 1)];
        line.backgroundColor = BackgroundColor;
        [contentView addSubview:line];
        
        DCImageAndTextLabel *textLabel = [[DCImageAndTextLabel alloc]initWithFrame:CGRectMake(60, line.bottom, contentView.width-60, 40)];
        textLabel.imageView.image = IMAGENAME(@"问号");
        textLabel.textLabel.text = @"请选择车辆和司机";
        [contentView addSubview:textLabel];
        
        self.plateField = [[UITextField alloc]init];
//        self.plateField.backgroundColor = RandomColor;
        self.plateField.size = CGSizeMake(contentView.width/2, 40);
        self.plateField.x = (contentView.width-self.plateField.width)/2;
        self.plateField.y = textLabel.bottom+5;
        self.plateField.font = FONT(16);
        self.plateField.placeholder = @"模糊录入车辆信息进行查询";
        [contentView addSubview:self.plateField];
        
        UIImageView *plateImage = [[UIImageView alloc]init];
        plateImage.image = IMAGENAME(@"车牌");
        plateImage.size = CGSizeMake(35, 20);
        plateImage.x = CGRectGetMaxX(self.plateField.frame)-self.plateField.width-plateImage.width-5;
        plateImage.y = self.plateField.y + 10;
        [contentView addSubview:plateImage];
        
        UIView *line1 = [[UIView alloc]initWithFrame:CGRectMake(self.plateField.x, self.plateField.bottom, self.plateField.width, 1)];
        line1.backgroundColor = BackgroundColor;
        [contentView addSubview:line1];
        
        UIImageView *driverImage = [[UIImageView alloc]init];
        driverImage.image = IMAGENAME(@"司机_blue");
        driverImage.size = CGSizeMake(35, 40);
        driverImage.x = plateImage.x;
        driverImage.y = self.plateField.bottom;
        [contentView addSubview:driverImage];
        
        self.nameField = [[UITextField alloc]initWithFrame:CGRectMake(self.plateField.x, line1.bottom, self.plateField.width, self.plateField.height)];
//        self.nameField.imageView.image = IMAGENAME(@"用户头像");
        self.nameField.placeholder = @"请选择司机";
        self.nameField.font = FONT(16);
        [contentView addSubview:self.nameField];
        
        UIView *line2 = [[UIView alloc]initWithFrame:CGRectMake(self.plateField.x, self.nameField.bottom, self.plateField.width, 1)];
        line2.backgroundColor = BackgroundColor;
        [contentView addSubview:line2];
        
        __weak typeof(self) weakself = self;
        UIButton *searchBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        searchBtn.backgroundColor = BlueColor;
        searchBtn.layer.cornerRadius = 4;
        searchBtn.clipsToBounds = YES;
        [searchBtn setTitle:@"搜索" forState:UIControlStateNormal];
        searchBtn.titleLabel.font = FONT(15);
        [searchBtn addTarget:self action:@selector(searchBtnDidClick) forControlEvents:UIControlEventTouchUpInside];
        [contentView addSubview:searchBtn];
        [searchBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(textLabel.mas_bottom).offset(5);
            make.left.equalTo(weakself.plateField.mas_right).offset(5);
            make.width.mas_offset(50);
            make.height.mas_offset(30);
        }];
        
        CGFloat btnWidth = (contentView.width - 100*WIDTHSCALE6)/2;
        UIButton * surtBtn = [[UIButton alloc]init];
        [surtBtn setTitle:@"提交" forState:UIControlStateNormal];
        [surtBtn setTitleColor:BlueColor forState:UIControlStateNormal];
        surtBtn.titleLabel.font = [UIFont systemFontOfSize:18];
        [surtBtn addTarget:self action:@selector(surtBtnOnClicked:) forControlEvents:UIControlEventTouchUpInside];
        surtBtn.layer.cornerRadius = 18;
        surtBtn.layer.masksToBounds = YES;
        surtBtn.layer.borderWidth = 1;
        surtBtn.layer.borderColor = BlueColor.CGColor;
        [contentView addSubview:surtBtn];
        [surtBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_offset(btnWidth);
            make.height.mas_offset(35);
            make.left.equalTo(contentView.mas_left).offset(40);
            make.bottom.equalTo(contentView.mas_bottom).offset(-10);
        }];
        
        UIButton * cancleBtn = [[UIButton alloc]init];
        [cancleBtn setTitle:@"取消" forState:UIControlStateNormal];
        cancleBtn.titleLabel.font = [UIFont systemFontOfSize:18];
        [cancleBtn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
        [cancleBtn addTarget:self action:@selector(cancleBtnOnClicked:) forControlEvents:UIControlEventTouchUpInside];
        cancleBtn.layer.cornerRadius = 18;
        cancleBtn.layer.masksToBounds = YES;
        cancleBtn.layer.borderWidth = 1;
        cancleBtn.layer.borderColor = [UIColor darkGrayColor].CGColor;
        [contentView addSubview:cancleBtn];
        [cancleBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_offset(btnWidth);
            make.height.mas_offset(35);
            make.right.equalTo(contentView.mas_right).offset(-40);
            make.bottom.equalTo(contentView.mas_bottom).offset(-10);
        }];
    }
    return _bgView;
}
-(void)surtBtnOnClicked:(UIButton *)sender {
    
}
-(void)searchBtnDidClick {
    
}
-(void)cancleBtnOnClicked:(UIButton *)sender {
    UIView *view = (UIView *)self.plateField.superview.superview;
    view.transform = CGAffineTransformIdentity;
    [UIView animateWithDuration:.5 animations:^{
        view.transform = CGAffineTransformMakeScale(0.0000000001, 0.00000001);
    }completion:^(BOOL finished) {
        [view removeFromSuperview];
    }];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
